<?php

/**
 * @author gbfun <gbfun@163.com>  2017-09-16 20:03
 * http://www.wuweiyou.com
 */

class formatter_good
{    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }
    
    //商品名称
    public function set_by_name(& $row)
    {
        $name = $row['name'];   
        
        $row['name_sub'] = $this->_get_name_sub($name);
    }
    
    protected function _get_name_sub($name)
    {
        //$name = mb_substr($name, 0, 25, 'utf-8');   //by gbfun 测试最适合长度    
		if (mb_strlen($name, 'utf-8') > 25) {     			
			$name_sub = mb_substr($name, 0, 25, 'utf-8') . '...';
		}else{
		    $name_sub = $name;
		}
    
        return $name_sub;
    }
    
    public function set_by_create_time(& $row)
    {
        $create_time = $row['create_time'];
    
        $row['create_time_date_time'] = $this->_get_create_time_date_time($create_time);
        $row['create_time_date']      = $this->_get_create_time_date($create_time);
        $row['create_time_time']      = $this->_get_create_time_time($create_time);
    }
    
    public function set_by_create_time_date_time(& $row)
    {
        $create_time = strtotime($row['create_time_date_time']);
    
        $row['create_time']      = $create_time;
        $row['create_time_date'] = $this->_get_create_time_date($create_time);
        $row['create_time_time'] = $this->_get_create_time_time($create_time);
    }
    
    //protected function _get_create_time_date_time_by_create_time($create_time)
    protected function _get_create_time_date_time($create_time)
    {
        $create_time_date_time = date('Y-m-d H:i:s', $create_time);
    
        return $create_time_date_time;
    }
    
    //protected function _get_create_time_date_by_create_time($create_time)
    protected function _get_create_time_date($create_time)
    {
        $create_time_date = date('Y-m-d', $create_time);
    
        return $create_time_date;
    }
    
    //protected function _get_create_time_time_by_create_time($create_time)
    protected function _get_create_time_time($create_time)
    {
        $create_time_time = date('H:i:s', $create_time);
    
        return $create_time_time;
    }   
    
    
    public function set_by_update_time(& $row)
    {
        $update_time = $row['update_time'];
    
        $row['update_time_date_time'] = $this->_get_update_time_date_time($update_time);
        $row['update_time_date']      = $this->_get_update_time_date($update_time);
        $row['update_time_time']      = $this->_get_update_time_time($update_time);
    }
    
    public function set_by_update_time_date_time(& $row)
    {
        $update_time = strtotime($row['update_time_date_time']);
        
        $row['update_time']      = $update_time;
        $row['update_time_date'] = $this->_get_update_time_date($update_time);
        $row['update_time_time'] = $this->_get_update_time_time($update_time);
    }
    
    //protected function _get_update_time_date_time_by_update_time($update_time)
    protected function _get_update_time_date_time($update_time)
    {
        $update_time_date_time = date('Y-m-d H:i:s', $update_time);
    
        return $update_time_date_time;
    }
    
    //protected function _get_update_time_date_by_update_time($update_time)
    protected function _get_update_time_date($update_time)
    {
        $update_time_date = date('Y-m-d', $update_time);
    
        return $update_time_date;
    }
    
    //protected function _get_update_time_time_by_update_time($update_time)
    protected function _get_update_time_time($update_time)
    {
        $update_time_time = date('H:i:s', $update_time);
    
        return $update_time_time;
    }
    
    public function set_by_status(& $row)
    {
        $status = $row['status'];
    
        $row['status_desc'] = $this->_get_status_desc($status);
    }
    
    //protected function _get_create_time_date_time_by_create_time($create_time)
    protected function _get_status_desc($status)
    {
        $status_lookup = array(0 => '禁用', '1' => '启用');
        $status_desc  = $status_lookup[$status];
    
        return $status_desc;
    }
    
    //图片
    public function set_by_image(& $row)
    {
        $image     = $row['image'];
        
        $image_arr = $this->_get_image_arr($image);
    
        $row['image_arr']    = $image_arr;
        $row['image_origin'] = $this->_get_image_origin($image_arr);
        $row['image_thumb']  = $this->_get_image_thumb($image_arr);
        $row['image_norm']   = $this->_get_image_norm($image_arr);
    }
    
    public function set_by_image_arr(& $row)
    {
        $image_arr = $row['image_arr'];
    
        $row['image']        = $this->_get_image($image_arr);
        $row['image_origin'] = $this->_get_image_origin($image_arr);
        $row['image_thumb']  = $this->_get_image_thumb($image_arr);
        $row['image_norm']   = $this->_get_image_norm($image_arr);
    }
    
    protected function _get_image_arr($image)
    {
        $image_arr = explode(',', $image);
    
        return $image_arr;
    }
    
    protected function _get_image($image_arr)
    {
        $image = implode(',', $image_arr);
    
        return $image;
    }
    
    //protected function _get_create_time_date_time_by_create_time($create_time)
    protected function _get_image_origin($image_arr)
    {
        $image_origin_arr = array();
        
        foreach($image_arr as $image_item){
    		if(!empty($image_item)){
    			$image_origin_arr[] = '/module/common/upload/images/good/' . $image_item;
    		}else{
    			$image_origin_arr[] = '';
    		}
    	}
    
        return $image_origin_arr;
    }
    
    //protected function _get_create_time_date_time_by_create_time($create_time)
    protected function _get_image_thumb($image_arr)
    {
        $image_thumb_arr = array();
        
        foreach($image_arr as $image_item){
    		if(!empty($image_item)){
    		    $image_item        = str_replace('/origin/', '/thumb/', $image_item);
    			$image_thumb_arr[] = '/module/common/upload/images/good/' . $image_item;
    		}else{
    			$image_thumb_arr[] = '';
    		}
    	}
    
        return $image_thumb_arr;
    }
    
    //protected function _get_create_time_date_time_by_create_time($create_time)
    protected function _get_image_norm($image_arr)
    {
        $image_norm_arr = array();
        
        foreach($image_arr as $image_item){
    		if(!empty($image_item)){
    		    $image_item       = str_replace('/origin/', '/norm/', $image_item);
    			$image_norm_arr[] = '/module/common/upload/images/good/' . $image_item;
    		}else{
    			$image_norm_arr[] = '';
    		}
    	}
    
        return $image_norm_arr;
    }
}

